package com.ultrapower.ctool.xn_app;

import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.ultrapower.ctool.ConfUtils;
import com.ultrapower.ctool.bean.TotalRowsPair;
import com.ultrapower.ctool.core.CustomApplication;
import com.ultrapower.ctool.db.SqlHelpDemo;
import com.ultrapower.ctool.http.BaseActivity;
import com.ultrapower.ctool.utils.DialogDemo;

import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ChangePasswordActivity extends BaseActivity {
    private EditText oldPassword;//旧密码
    private EditText newPassword;//新密码
    private EditText checkPassword;//确认密码
    private Button changePasswordButton;
    //private OkHttpClient client = new OkHttpClient();
    private ProgressDialog progressDialog;
    private Gson gson = new Gson();
    private CustomApplication application;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    closeProgressDialog();
                    Toast.makeText(getApplicationContext(), "密码修改成功", Toast.LENGTH_SHORT).show();
                    //Intent intent = new Intent(ChangePasswordActivity.this, LoginActivity.class);
                    Intent intent = new Intent();
                    intent.setClass(ChangePasswordActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                    break;
                case 0:
                    closeProgressDialog();
                    Toast.makeText(getApplicationContext(), "密码修改失败", Toast.LENGTH_SHORT).show();
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        oldPassword = (EditText) findViewById(R.id.oldPassword);
        newPassword = (EditText) findViewById(R.id.newPassword);
        checkPassword = (EditText) findViewById(R.id.checkPassword);
        changePasswordButton = (Button) findViewById(R.id.changePasswordButton);
        application = (CustomApplication) getApplication();
        changePasswordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String userOldPw = oldPassword.getText().toString().trim();
                final String userNewPw = newPassword.getText().toString().trim();
                String userCheckNewPw = checkPassword.getText().toString().trim();
                if ("".equals(userOldPw) || "".equals(userNewPw) || "".equals(userCheckNewPw)) {
                    Toast.makeText(getApplicationContext(), "密码不能为空", Toast.LENGTH_SHORT).show();
                } else if (!userNewPw.equals(userCheckNewPw)) {
                    Toast.makeText(getApplicationContext(), "两次密码不一致", Toast.LENGTH_SHORT).show();
                } else if (!"".equals(userOldPw) && userOldPw.equals(userNewPw)) {
                    Toast.makeText(getApplicationContext(), "新密码不能与旧密码相同", Toast.LENGTH_SHORT).show();
                } else {
                    showProgressDialog();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                OkHttpClient client = new OkHttpClient();
                                RequestBody requestBody = new FormBody.Builder()
                                        .add("oldPwd", userOldPw)
                                        .add("newPwd", userNewPw)
                                        .add("userId", String.valueOf(application.getLoginUser().getLogin_id()))
                                        .build();
                                Request request = new Request.Builder().url(ConfUtils.SERVICE_URL + "user/updatePwd").post(requestBody).build();
                                Response response = client.newCall(request).execute();
                                if (response.isSuccessful()) {//成功
                                    String result = response.body().string();
                                    System.out.println(result);
                                    Map<String, Object> map = gson.fromJson(result, Map.class);
                                    if ((boolean) map.get("success")) {
                                        Message message = new Message();
                                        message.what = 1;
                                        handler.sendMessage(message);
                                    } else {
                                        Message message = new Message();
                                        message.what = 0;
                                        handler.sendMessage(message);
                                    }
                                }

                            } catch (Exception e) {
                                Message message = new Message();
                                message.what = 0;
                                handler.sendMessage(message);
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }
            }
        });
    }

    /**
     * 显示进度对话框
     */
    private void showProgressDialog() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(ChangePasswordActivity.this);
            progressDialog.setMessage("正在加载，请稍等...");
            progressDialog.setCanceledOnTouchOutside(false);
        }
        progressDialog.show();
    }

    /**
     * 关闭进度对话框
     */
    private void closeProgressDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }
}
